package com.kerbores.utils.baidu.location;

import java.util.HashMap;
import java.util.Map;

import org.nutz.json.Json;
import org.nutz.lang.Lang;
import org.nutz.lang.Strings;
import org.nutz.lang.util.NutMap;
import org.nutz.log.Logs;

import com.kerbores.utils.baidu.common.BaiduApiInvoker;
import com.kerbores.utils.baidu.common.JsonBean;
import com.kerbores.utils.entries.Result;

/**
 * 大量百度免费api的封装
 * 
 * @author 王贵源
 * @email kerbores@kerbores.com
 *
 *        create at 2015年10月15日 下午3:22:04
 */
public class Baidus extends BaiduApiInvoker {

	public static class Location extends JsonBean {
		public static Location defaultLocation = new Location(106.53063501, 29.54460611);
		// 经度
		private double longitude;
		// 维度
		private double latitude;
		/**
		 * 省份
		 */
		private String province;
		/**
		 * 城市
		 */
		private String city;
		/**
		 * 地区
		 */
		private String district;

		/**
		 * @return the province
		 */
		public String getProvince() {
			return province;
		}

		/**
		 * @param province
		 *            the province to set
		 */
		public void setProvince(String province) {
			this.province = province;
		}

		/**
		 * @return the city
		 */
		public String getCity() {
			return city;
		}

		/**
		 * @param city
		 *            the city to set
		 */
		public void setCity(String city) {
			this.city = city;
		}

		/**
		 * @return the district
		 */
		public String getDistrict() {
			return district;
		}

		/**
		 * @param district
		 *            the district to set
		 */
		public void setDistrict(String district) {
			this.district = district;
		}

		/**
		 * @return the longitude
		 */
		public double getLongitude() {
			return longitude;
		}

		/**
		 * @param longitude
		 *            the longitude to set
		 */
		public void setLongitude(double longitude) {
			this.longitude = longitude;
		}

		/**
		 * @return the latitude
		 */
		public double getLatitude() {
			return latitude;
		}

		/**
		 * @param latitude
		 *            the latitude to set
		 */
		public void setLatitude(double latitude) {
			this.latitude = latitude;
		}

		/**
		 * @param longitude
		 * @param latitude
		 */
		public Location(double longitude, double latitude) {
			super();
			this.longitude = longitude;
			this.latitude = latitude;
		}

	}

	public static void main(String[] args) {
		System.err.println(ip2Address("119.85.119.145"));
	}

	/**
	 * 请求者经纬度
	 * 
	 * @return
	 */
	public static Location ip2Location() {
		return ip2Location(null);
	}

	/**
	 * 具体ip的经纬度
	 * 
	 * @param ip
	 * @return
	 */
	public static Location ip2Location(String ip) {
		Logs.get().debug("ip:" + ip);
		if (Strings.equalsIgnoreCase("localhost", ip) || Strings.equalsIgnoreCase(ip, "127.0.0.1") || Strings.equalsIgnoreCase(ip, "0:0:0:0:0:0:0:1")) {
			ip = "119.85.119.145";
		}
		NutMap params = NutMap.NEW();
		Map<String, String> properties = new HashMap<String, String>();
		properties.put("apikey", "4a6cfff3a7840913094a7cbb67d79537");
		params.put("ip", ip);
		params.put("coor", "bd09ll");
		Result result = get("4a6cfff3a7840913094a7cbb67d79537", "http://apis.baidu.com/apistore/lbswebapi/iplocation", params);
		if (result.isSuccess()) {
			NutMap ret = result.getData();
			NutMap content = ret.getAs("content", NutMap.class);
			NutMap point = content.getAs("point", NutMap.class);
			Location location = new Location(point.getDouble("x"), point.getDouble("y"));
			NutMap address_detail = content.getAs("address_detail", NutMap.class);
			location.setProvince(address_detail.getString("province"));
			location.setCity(address_detail.getString("city"));
			location.setDistrict(address_detail.getString("district"));
			return location;
		}
		return Location.defaultLocation;
	}

	/**
	 * IP转换为地址信息
	 * 
	 * @param ip
	 * @return
	 */
	public static Address ip2Address(String ip) {
		Logs.get().debug("ip:" + ip);
		if (Strings.equalsIgnoreCase("localhost", ip) || Strings.equalsIgnoreCase(ip, "127.0.0.1") || Strings.equalsIgnoreCase(ip, "0:0:0:0:0:0:0:1")) {
			ip = "119.85.119.145";
		}
		NutMap params = NutMap.NEW();
		params.put("ip", ip);

		Result result = get("4a6cfff3a7840913094a7cbb67d79537", "http://apis.baidu.com/apistore/iplookupservice/iplookup", params);
		if (result.isSuccess()) {
			return Lang.map2Object(result.getData(), Address.class);
		}
		return null;

	}

	public static class Address {
		/**
		 * ip
		 */
		private String ip;
		/**
		 * 国家
		 */
		private String country;
		/**
		 * 省份
		 */
		private String province;
		/**
		 * 城市
		 */
		private String city;
		/**
		 * 地区
		 */
		private String district;
		/**
		 * 运营商
		 */
		private String carrier;

		/**
		 * @return the ip
		 */
		public String getIp() {
			return ip;
		}

		/**
		 * @param ip
		 *            the ip to set
		 */
		public void setIp(String ip) {
			this.ip = ip;
		}

		/**
		 * @return the country
		 */
		public String getCountry() {
			return country;
		}

		/**
		 * @param country
		 *            the country to set
		 */
		public void setCountry(String country) {
			this.country = country;
		}

		/**
		 * @return the province
		 */
		public String getProvince() {
			return province;
		}

		/**
		 * @param province
		 *            the province to set
		 */
		public void setProvince(String province) {
			this.province = province;
		}

		/**
		 * @return the city
		 */
		public String getCity() {
			return city;
		}

		/**
		 * @param city
		 *            the city to set
		 */
		public void setCity(String city) {
			this.city = city;
		}

		/**
		 * @return the district
		 */
		public String getDistrict() {
			return district;
		}

		/**
		 * @param district
		 *            the district to set
		 */
		public void setDistrict(String district) {
			this.district = district;
		}

		/**
		 * @return the carrier
		 */
		public String getCarrier() {
			return carrier;
		}

		/**
		 * @param carrier
		 *            the carrier to set
		 */
		public void setCarrier(String carrier) {
			this.carrier = carrier;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see java.lang.Object#toString()
		 */
		@Override
		public String toString() {
			return Json.toJson(this);
		}

	}

}
